//
//  KLCateViewController.m
//  used
//
//  Created by xalo on 16/7/13.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "KLCateViewController.h"

@interface KLCateViewController ()



@property (nonatomic, strong) UIView *bgView;

@end

@implementation KLCateViewController

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight)];
    }
    return _bgView;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"类别";

    [self.view addSubview:self.bgView];

    [self addCateButton];
    
}

- (void)addCateButton {
    for (int i = 1; i < self.cates.count; i++) {

        // 列数
        int maxCols = 3;
        // 宽度和高度
        CGFloat buttonW = self.view.width / maxCols;
        CGFloat buttonH = buttonW;
        // 创建按钮
        KLButton *button = [KLButton buttonWithType:UIButtonTypeCustom];
        button.adjustsImageWhenHighlighted = NO;
        button.showsTouchWhenHighlighted = NO;
        // 监听点击
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        // 传递模型
        button.listModel = self.cates[i];
        button.tag = i;//[[self.cates[i] hotcategoryid] integerValue];
        if ([button.listModel.hotcategoryimgurl isEqualToString:@""]) continue;
        
        // 计算frame
        int col = (i - 1) % maxCols;
        int row = (i - 1) / maxCols;
        button.x = col * buttonW;
        button.y = row * buttonH;
        button.width = buttonW;
        button.height = buttonH;
        [self.bgView addSubview:button];
    }

}


- (void)buttonClick:(UIButton *)button {
    self.block(button.tag);
    
    [self.navigationController popViewControllerAnimated:YES];
}


@end
